package com.control.qct.businesssteward.fragment;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.activity.SubmitTheCargoActivity;
import com.control.qct.businesssteward.adapter.SponsorTheCargoAdapter;
import com.control.qct.businesssteward.base.BaseFragment;
import com.control.qct.businesssteward.model.SponsorTheCargoModel;
import com.control.qct.businesssteward.utils.CustomCodeUtils;
import com.control.qct.businesssteward.utils.DialogUtil;
import com.control.qct.businesssteward.utils.EditTextUtils;
import com.control.qct.businesssteward.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 发起调拨
 * Created by Administrator on 2017/12/16 0016.
 */

public class SponsorTheCargoFragment extends BaseFragment implements CustomCodeUtils.ICallbackListener{
    @BindView(R.id.iv_goods_style)
    ImageView mIvGoodsStyle;//商品显示样式
    @BindView(R.id.et_barcode)
    EditText mEtBarcode;//条形码输入框
    @BindView(R.id.iv_sure)
    ImageView mIvSure;//确认条形码
    @BindView(R.id.iv_scan)
    ImageView mIvScan;//扫描条形码
    @BindView(R.id.iv_clear)
    ImageView mIvClear;
    @BindView(R.id.rv_order_goods)
    RecyclerView mRvSponsorOrderGoods;

    private List<SponsorTheCargoModel> mGoodsBeanList;
    private SponsorTheCargoAdapter mSponsorTheCargoAdapter;
    private View mTheCargoNumView;
    private Dialog mTheCargoNumDialog;
    private Bundle mBundle;

    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        return inflater.inflate(R.layout.fragment_sponsor_the_cargo, container, false);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    protected void initView(View view) {
        mBundle = new Bundle();
        mGoodsBeanList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            mGoodsBeanList.add(new SponsorTheCargoModel());
        }
        mRvSponsorOrderGoods.setLayoutManager(new LinearLayoutManager(activity));
        mSponsorTheCargoAdapter = new SponsorTheCargoAdapter(mGoodsBeanList);
        mRvSponsorOrderGoods.setAdapter(mSponsorTheCargoAdapter);
        mSponsorTheCargoAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                setTheCargoNumDialog();
            }
        });
        EditTextUtils.isEditContent(mEtBarcode, mIvClear);
    }

    @OnClick({R.id.iv_goods_style, R.id.iv_sure, R.id.iv_scan, R.id.iv_clear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_goods_style://分类
                break;
            case R.id.iv_sure://确认搜索
                break;
            case R.id.iv_clear://清除
                mEtBarcode.setText("");
                break;
            case R.id.iv_scan://扫描
                CustomCodeUtils.getInstance(activity).startToCaptureActivity(this);
                break;
        }
    }

    /**
     * 调货量弹窗
     */
    private void setTheCargoNumDialog() {
        mTheCargoNumView = View.inflate(activity, R.layout.dialog_change_price,null);
        mTheCargoNumDialog = DialogUtil.getDialog(activity, true, mTheCargoNumView, ScreenUtil
                .getScreenWidthPix(activity));
        TextView tvDialogTitle = mTheCargoNumView.findViewById(R.id.tv_dialog_title);
        TextView tvGoodsName = mTheCargoNumView.findViewById(R.id.tv_goods_name);
        TextView tvGoodsCode = mTheCargoNumView.findViewById(R.id.tv_goods_code);
        TextView tvGoodsPrice = mTheCargoNumView.findViewById(R.id.tv_goods_price);
        EditText etChangePrice = mTheCargoNumView.findViewById(R.id.et_change_price);
        tvDialogTitle.setText("调货量");
        etChangePrice.setHint("请输入调拨数");
        mTheCargoNumView.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBundle.putInt("id",1);
                startActivity(SubmitTheCargoActivity.class,mBundle);
                mTheCargoNumDialog.dismiss();
            }
        });
        mTheCargoNumView.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBundle.putInt("id",2);
                startActivity(SubmitTheCargoActivity.class,mBundle);
                mTheCargoNumDialog.dismiss();
            }
        });
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        CustomCodeUtils.getInstance(activity).onCallBackListener(requestCode, data,this);
    }

    @Override
    public void onCallbackListener(String result) {

    }
}
